/*
 * @lc app=leetcode.cn id=434 lang=cpp
 *
 * [434] 字符串中的单词数
 */

// @lc code=start
class Solution
{
public:
    int countSegments(string s)
    {
        if (s.size() == 0)
            return 0;
        int ans = 0;
        int i = 0, j = 0;

        while (i < s.size() && s[i] == ' ')
            ++i;
        j = i + 1;

        while (j < s.size())
        {
            while (j < s.size() && s[j] != ' ')
                ++j;
            ans++;

            i = j + 1;
            while (i < s.size() && s[i] == ' ')
                ++i;
            j = i + 1;
        }
        if(i < s.size()) ans++;
        return ans;
    }
};
// @lc code=end
